/**
 * @param {number[][]} matrix
 * @param {number} k
 * @return {number}
 */

class Queue{
    constructor(k){
        this._queue=new Array(k).fill(-1);
        this.length=k;
        this.fill=0;
    }
    insert(val){
        let i=0;
        while(i<this.fill&&val<=this._queue[i]){
            i++;
        }
        let j=this.fill<this.length ? this.fill:this.fill-1;
        while(j>i){
            this._queue[j]=this._queue[j-1];
            j--;
        }
        this._queue[i]=val;
        if(this.fill<this.length) this.fill++;
    }
    get(){
        return this._queue[this.length-1];
    }
}

 var kthLargestValue = function(matrix, k) {
    let h=matrix.length,w=matrix[0].length;
    let xM=new Array(h).fill(0).map(() => new Array(w).fill(0));
    let q=new Queue(k);
    for(let i=0;i<h;i++){
        for(let j=0;j<w;j++){
            xM[i][j]=matrix[i][j] ^ (i-1>=0 ? xM[i-1][j]:0) ^ (j-1>=0 ? xM[i][j-1]:0) ^ (i-1>=0&&j-1>=0 ? xM[i-1][j-1]:0);
            
            q.insert(xM[i][j])
        }
    }
    return q.get();
};


console.log(kthLargestValue([[5,2],[1,6]],4))

//js碰到优先队列的题直接可以放弃，除非自己手写个堆